//
//  WBHomeViewController.swift
//  GitWeibo
//
//  Created by 胡金东 on 2017/5/18.
//  Copyright © 2017年 胡金东. All rights reserved.
//

import UIKit

/// - 定义原创 cell ID
private let origianlCellID = "origianlCellID"
/// - 定义转发 Cell ID
private let retweenCellID = "retweenCellID"

class WBHomeViewController: WBBaseViewController {

    /// - 列表视图模型
   lazy var listViewModel = WBStatusListViewModel()
    /// - 加载数据
    override func loadData() {
        /// - 进来就刷新
         refresh?.beginRefreshing()
        
       print("准备刷新最后一条数据\(self.listViewModel.statusList.last?.status.text)")
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2){
            
            
            self.listViewModel.loadStatus(isPull: self.isPullUp) { (isSuccess,shouldRefresh) in
                /// - 停止刷新
                self.refresh?.endRefreshing()
                /// - 恢复上拉刷新标记
                self.isPullUp = false
                /// - 刷新数据
                // 刷新表格
                if shouldRefresh {
                    self.tableView?.reloadData()
                }
                
            }
        }
        
        
       
    }
    /// - 返回按钮
    func navLeftClick()  {
        let  demo  = WBDemoViewController()
        /// - push 后 隐藏下面的tabBar
//        demo.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(demo, animated: true)
        
    }
    
    
    

}
//MARK: -  数据源
extension WBHomeViewController{

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
       /// - 获取内容
        let VM = listViewModel.statusList[indexPath.row]
        /// - 判断用哪个原创和转发cell 
        /**
         行高的缓存是解决性能最佳的途径
         */
        /// - 如果没有缓存行高，找到cell 按照自动布局的规则，从上向下计算，找到向下的约束，从而计算动态行高
        let cellID = (VM.status.retweeted_status != nil) ? retweenCellID : origianlCellID
        
        /// - 取cell
       let cell =  tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! WBHomeTableViewCell
        /// - 设置cell
        cell.viewModel = VM
        
    
        
    return cell
    }
    /// 父类必须实现代理方法，就是在WBBaseViewController控制器
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        /// - 1.根据 indexPath 获取视图模型
        let vm = listViewModel.statusList[indexPath.row]
        
        /// - 2.返回计算好的行高
        return vm.rowHeight
    }
    
}

//MARK: -  设置界面
extension WBHomeViewController{
    
    override func setUpTableView() {
        
        super.setUpTableView()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", titleColorNormal: UIColor.purple, titleColorHigh: UIColor.orange, titleFont: 16, target: self, action: #selector(navLeftClick))
        /// - 以后修改navItem ，不用再修改navigationItem
//         navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", titleColorNormal: UIColor.purple, titleColorHigh: UIColor.orange, titleFont: 16, target: self, action: #selector(navLeftClick))
        
        //TODO: - tableView 注册
    
        tableView?.register(UINib(nibName:"WBHomeTableViewNormalCell", bundle: nil), forCellReuseIdentifier: origianlCellID)
        tableView?.register(UINib(nibName:"WBHomeTableViewRetweenCell", bundle: nil), forCellReuseIdentifier: retweenCellID)
        
        /// - 设置行高
//        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight  = 300
        /// - 取消分割线
        tableView?.separatorStyle = .none
        
        setUpNavTitle()
    }
    
    //TODO: - 设置导航用户昵称
    func setUpNavTitle() {
        let title = WBNetWorkSessionManager.NetWorkshared().account.screen_name
        let button = WBTitleButon(title: title)
        navItem.titleView = button
        button.addTarget(self, action: #selector(TitleButtonClick), for: .touchUpInside)
        
        
    }
    
    @objc func TitleButtonClick(btn:UIButton) {
        
        btn.isSelected = !btn.isSelected
        
    }
}
